using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using gfoidl.Imaging;




namespace ScreenshotCapturer
{
    public partial class ShotCropper : Form
    {
        private Image _originalImage;
        private bool _selecting;
        private Rectangle _selection;

        public ShotCropper()
        {
            InitializeComponent();
            try
            {
                pictureBox1.ImageLocation = "temp.png";
            }
            catch
            { }
            
        }

        private void ShotCropper_Load(object sender, EventArgs e)
        {
           
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
        private void ShotCropper_Resize(object sender, EventArgs e)
        {
            try
            {
                pictureBox1.Location = new Point(0, 25);
            }
            catch
            { }
        }
        
      

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                // Update the actual size of the selection:
                if (_selecting)
                {
                    _selection.Width = e.X - _selection.X;
                    _selection.Height = e.Y - _selection.Y;
                    // Redraw the picturebox:
                    pictureBox1.Refresh();
                }
            }
            catch
            {
            }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left && _selecting)
                {
                    _selecting = false;
                }
            }
            catch
            {
            }
           
        }
       
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                if (_selecting)
                {
                    // Draw a rectangle displaying the current selection
                    Pen pen = Pens.Red;
                    e.Graphics.DrawRectangle(pen, _selection);
                }
            }
            catch
            {
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                _originalImage = pictureBox1.Image.Clone() as Image;
                if (e.Button == MouseButtons.Left)
                {
                    _selecting = true;
                    _selection = new Rectangle(new Point(e.X, e.Y), new Size());
                }
            }
            catch
            {
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                // Create cropped image:
                Image img = pictureBox1.Image.Crop(_selection);

                // Fit image to the picturebox:
                // pictureBox1.Image = img.Fit2PictureBox(pictureBox1);
                pictureBox1.Image = img;
            }
            catch
            {
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            try
            {
                Image img = pictureBox1.Image;
                pictureBox1.Image = img.Fit2PictureBox(pictureBox1);
            }
            catch
            {
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sf = new SaveFileDialog();
                sf.Filter = "JPEG | *.jpg";
                sf.ShowDialog();
                pictureBox1.Image.Save(sf.FileName.ToString(), ImageFormat.Jpeg);
            }
            catch
            {
            }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            try
            {
                Image i = _originalImage.Clone() as Image;
                pictureBox1.Image = i;
            }
            catch
            {
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetImage(pictureBox1.Image);
            }
            catch { }
        }

        private void cropToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton2.PerformClick();

        }

        private void autoSizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton4.PerformClick();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton1.PerformClick();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton3.PerformClick();
        }

        private void originalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton5.PerformClick();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                About a = new About();
                a.Show();
            }
            catch
            {
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}